/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.woxthebox.draglistview.sample.fraction;

import com.woxthebox.draglistview.basecomponent.BaseListContainer;
import com.woxthebox.draglistview.sample.ResourceTable;
import com.woxthebox.draglistview.sample.adapter.HorizontalListAdapter;
import com.woxthebox.draglistview.sample.bean.HorizontalBean;
import com.woxthebox.draglistview.sample.untils.DataUntils;
import com.woxthebox.draglistview.utils.FinalStaticBean;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.TableLayoutManager;
import ohos.hiviewdfx.HiLog;

import java.util.List;

/**
 * HorizontalFractionAbility类
 *
 * @since 2021-04-21
 */
public class HorizontalFractionAbility extends Fraction {
    private BaseListContainer baseListContainer;
    private List<HorizontalBean> list;
    private AbilitySlice slice;
    private HorizontalListAdapter adapter;
    private boolean isEnable = true;

    /**
     * HorizontalFractionAbility构造方法
     *
     * @param slice
     */
    public HorizontalFractionAbility(AbilitySlice slice) {
        this.slice = slice;
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_horizontal_fraction_layout, null, false);
        baseListContainer = (BaseListContainer) component
                .findComponentById(ResourceTable.Id_horizontal_baselistcontainer);
        return component;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        DataUntils.setmTitleFlag(1);
        HiLog.info(FinalStaticBean.DRAGLISTVIEW, "onStart");
        TableLayoutManager tableLayout = new TableLayoutManager();
        tableLayout.setOrientation(Component.VERTICAL);
        tableLayout.setRowCount(1);
        baseListContainer.setLayoutManager(tableLayout);
        list = DataUntils.getHorizontalBean();
        adapter = new HorizontalListAdapter(list, slice, FinalStaticBean.HORIZONTALLISTLAYOUT);
        baseListContainer.setItemProvider(adapter);
    }

    /**
     * 可在外部AblitySlice中调用setLayoutType设置相应操作
     *
     * @param type
     */
    public void setLayoutType(int type) {
        if (type == FinalStaticBean.GRIDLAYOUTHORIZONTALTYPE) {
            list = DataUntils.getHorizontalBean();
            adapter = new HorizontalListAdapter(list, slice, FinalStaticBean.HORIZONTALGRIDLAYOUT);
            adapter.setEnable(isEnable);
            baseListContainer.setItemProvider(adapter);
        } else if (type == FinalStaticBean.LINEARLAYOUTTYPE) {
            list = DataUntils.getHorizontalBean();
            adapter = new HorizontalListAdapter(list, slice, FinalStaticBean.HORIZONTALLISTLAYOUT);
            adapter.setEnable(isEnable);
            baseListContainer.setItemProvider(adapter);
        } else if (type == FinalStaticBean.ADDCOLUMN) {
            list.add(new HorizontalBean(DataUntils.getmTitleFlag(), DataUntils.getListHorizontalBean()));
            adapter.notifyDataSetItemInserted(list.size() - 1);
        } else if (type == FinalStaticBean.REMOVECOLUMN) {
            if (list.size() > 0) {
                list.remove(0);
                if (list.size() > 0) {
                    adapter.notifyDataSetItemRemoved(0);
                } else {
                    adapter.notifyDataChanged();
                }
            }
        } else if (type == FinalStaticBean.CLEARBOARD) {
            list.clear();
            adapter.notifyDataChanged();
        } else if (type == FinalStaticBean.ENABLEDRAG && adapter != null) {
            isEnable = true;
            adapter.setEnable(isEnable);
            adapter.notifyDataChanged();
        } else if (type == FinalStaticBean.DISABLEDRAG && adapter != null) {
            isEnable = false;
            adapter.setEnable(isEnable);
            adapter.notifyDataChanged();
        }
    }
}
